package jgl.vajra.kingkong.part.vehicle.activity;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.orhanobut.logger.Logger;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jgl.vajra.kingkong.R;
import jgl.vajra.kingkong.bean.CouponAvailableBean;
import jgl.vajra.kingkong.databinding.ActivityCouponAvailableBinding;
import jgl.vajra.kingkong.dialogFragment.UseRulesDialogFragment;
import jgl.vajra.kingkong.event.CityPickerEvent;
import jgl.vajra.kingkong.event.CouponAvailableEvent;
import jgl.vajra.kingkong.manager.AccountManager;
import jgl.vajra.kingkong.part.vehicle.contract.CouponAvailableContract;
import jgl.vajra.kingkong.part.vehicle.viewModel.CouponAvailableViewModel;
import jgl.vajra.kingkong.utils.RandomUtils;
import jgl.vajra.kingkong.widget.databindingadapter.BaseBindingItemPresenter;
import jgl.vajra.kingkong.widget.databindingadapter.SingleTypeBindingAdapter;
import jgl.vajra.kingkong.widget.mvvm.factory.CreateViewModel;
import jgl.vajra.kingkong.widget.mvvm.view.BaseMVVMActivity;
import jgl.vajra.kingkong.widget.retrofithelper.utils.AEACCoder;
import jgl.vajra.kingkong.widget.utils.UIUtils;

@CreateViewModel(CouponAvailableViewModel.class)
public class CouponAvailableActivity extends BaseMVVMActivity<CouponAvailableViewModel, ActivityCouponAvailableBinding> implements CouponAvailableContract.View, BaseBindingItemPresenter {

    private SingleTypeBindingAdapter<CouponAvailableBean> adapter;
    private UseRulesDialogFragment useRulesDialogFragment;
    private String currentAmount;
    private List<CouponAvailableBean> data;
    private long couponId;

    @Override
    public int getLayoutId() {
        return R.layout.activity_coupon_available;
    }

    @Override
    public void initView() {
        super.initView();
        mBinding.setView(this);
        //当前余额
        currentAmount = getIntent().getStringExtra("amount");
        couponId = getIntent().getLongExtra("couponId",0);

        // 不可嵌套滑动
        mBinding.recyclerView.setNestedScrollingEnabled(false);
        // 取消head 的 recycleView的焦点
        mBinding.recyclerView.setFocusableInTouchMode(false);
        //设置布局管理器
        mBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new SingleTypeBindingAdapter(this, null, R.layout.item_coupon_available);
        mBinding.recyclerView.setAdapter(adapter);
        adapter.setItemPresenter(this);

        requestNetData();

        if(couponId==0){
            mBinding.ivNoCoupons.setImageResource(R.drawable.ic_tick_select);
            mBinding.llView.setBackgroundResource(R.drawable.shape_gray_eb_10dp);
        }else {
            mBinding.ivNoCoupons.setImageResource(R.drawable.ic_tick_normal);
            mBinding.llView.setBackgroundResource(R.drawable.shape_while_10dp);
        }
    }

    private void requestNetData() {
        Map<String, Object> map = new HashMap<>();
        map.put("Authorization", AccountManager.getInstance().getLoginData(UIUtils.getContext()).authorization);
        mViewModel.getCouponsAvailableData(map);
    }

    //不使用
    @Override
    public void onNoCoupons() {
        for (int i = 0; i < data.size(); i++) {
            data.get(i).isClick = false;
        }

        adapter.refresh();
        mBinding.ivNoCoupons.setImageResource(R.drawable.ic_tick_select);
        mBinding.llView.setBackgroundResource(R.drawable.shape_gray_eb_10dp);

        EventBus.getDefault().post(new CouponAvailableEvent(0,""));
        finish();
    }

    //点击itemData
    @Override
    public void onDetails(CouponAvailableBean itemData) {
        if (!itemData.isAvailable) return;
        if (itemData.isClick) return;

        for (int i = 0; i < data.size(); i++) {
            data.get(i).isClick = false;
        }
        itemData.isClick = true;
        adapter.refresh();
        mBinding.ivNoCoupons.setImageResource(R.drawable.ic_tick_normal);
        mBinding.llView.setBackgroundResource(R.drawable.shape_while_10dp);

        EventBus.getDefault().post(new CouponAvailableEvent(itemData.id,AEACCoder.decrypt(itemData.amount)));
        finish();
    }

    //使用规则
    @Override
    public void onUseRules(CouponAvailableBean itemData) {
        if (useRulesDialogFragment == null)
            useRulesDialogFragment = new UseRulesDialogFragment();
        useRulesDialogFragment.setData(itemData);
        useRulesDialogFragment.show(getSupportFragmentManager());
    }

    @Override
    public void returnCouponsAvailableData(List<CouponAvailableBean> data) {
        this.data = data;
        int newCurrentAmount = Integer.parseInt(currentAmount);

        for (int i = 0; i < data.size(); i++) {
            if (data.get(i).type == 1) {
                if (newCurrentAmount > Integer.parseInt(data.get(i).price)) {
                    data.get(i).isAvailable = true;
                    if (couponId==data.get(i).id){
                        data.get(i).isClick=true;
                    }
                }
            } else {
                if (newCurrentAmount > Integer.parseInt(AEACCoder.decrypt(data.get(i).amount))) {
                    data.get(i).isAvailable = true;
                    if (couponId==data.get(i).id){
                        data.get(i).isClick=true;
                    }
                }
            }
        }
        adapter.refresh(data);
    }

    @Override
    public void onItemClick(int position, Object itemData) {

    }
}
